package edu.nju;

import org.apache.commons.cli.*;

public class CommandLineUtil {
    private static CommandLine commandLine;
    private static CommandLineParser parser = new DefaultParser();
    private static Options options = new Options();
    private boolean sideEffect=false;
    public static final String WRONG_MESSAGE = "Invalid input.";

    /**
     * you can define options here
     * or you can create a func such as [static void defineOptions()] and call it before parse input
     */
    static {
    }

    public void main(String[] args){
       options.addOption("h","help",false,"打印出所有预定义的选项与用法");
       options.addOption("p","print",true,"打印出arg");
       options.addOption("s",false,"将CommandlineUtil中sideEffect变量置为true");
       try{
       commandLine=parser.parse(options,args);
        if( commandLine.hasOption( "h" ) ) {
         printHelpMessage();
       }else
       if( commandLine.hasOption( "p" ) ) {
          String temp = commandLine.getOptionValue("p");
          String[] tempp=commandLine.getArgs();
          if (tempp.length==0){
               System.out.println(WRONG_MESSAGE);
          }else{
          System.out.println(temp);
          }
       }else
       if( commandLine.hasOption( "s" ) ) {
         this.sideEffect=true;
       }
       }catch(ParseException e){
           System.out.println(e.getMessage());
           System.exit(-1);
       }

    }

    /**
     * Print the usage of all options
     */
    private static void printHelpMessage() {
        System.out.println("help");
    }

    /**
     * Parse the input and handle exception
     * @param args origin args form input
     */
    public void parseInput(String[] args) {
    }

    /**
     * You can handle options here or create your own func
     */
    public void handleOptions() {
    }

    public boolean getSideEffectFlag(){
        return this.sideEffect;
    }

}
